package com.atguigu.eduservice.controller;



import com.alibaba.excel.util.StringUtils;
import com.atguigu.commonutils.ResultVo;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionHandler.GuliException;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-12-12
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;
    //注入vodClient
    @Autowired
    private VodClient vodClient;

    //添加小节
    @ApiOperation(value = "新增课时")
    @PostMapping("addVideo")
    public ResultVo addVideo(@RequestBody EduVideo eduVideo) {
        boolean save=videoService.save(eduVideo);
        return ResultVo.ok();
    }
    //根据小节ID查询课时
    @ApiParam(name = "id", value = "课时ID", required = true)
    @ApiOperation(value = "根据ID查询课时")
    @GetMapping("getVideoInfo/{id}")
    public ResultVo getVideoInfo(@PathVariable String id){
        EduVideo video = videoService.getById(id);
        return ResultVo.ok().data("video",video);
    }
    //修改小节
    @ApiOperation(value = "更新课时")
    @PostMapping("updateVideo")
    public ResultVo updateCourseInfo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return ResultVo.ok();
    }
    @ApiOperation(value = "根据ID删除课时")
    @ApiParam(name = "id", value = "课时ID", required = true)
    @DeleteMapping("{videoId}")
    public ResultVo deleteVideo(@PathVariable String videoId) {
        //根据小节id 获取 视频id 然后调用方法实现视频删除功能
        EduVideo eduVideo = videoService.getById(videoId);
        String videoSourceId = eduVideo.getVideoSourceId();
        //判断小节里面是否有视频id
        if (!StringUtils.isEmpty(videoSourceId)) {
            //根据视频id 实现远程调用删除阿里云视频
            ResultVo result = vodClient.removeAlyVideo(videoSourceId);
            if (result.getCode() == 20001) {
                throw new GuliException(20001,"删除视频失败，熔断器...");
            }
        }
        //删除小节
        videoService.removeById(videoId);
        return ResultVo.ok();
    }




}